package kosta.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import kosta.model.friends.FriendsService;
import kosta.model.friends.FriendsVO;
import kosta.model.member.MemberVO;
import kosta.model.memo.MemoVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



@Controller
public class FriendsController {
	@Resource(name="friendsServiceImpl")
	private FriendsService friendsService;
	
	@RequestMapping("/getFriendsList.do")
	public ModelAndView getFriendsList(String id) throws Exception{
		System.out.println(id + "-----------getFriendsList id");
		List<FriendsVO> flist = friendsService.getFriendsList(id);
		System.out.println(flist +"------------ getFriendsList ok");
		return new ModelAndView("getFriendsList.friends", "flist", flist);
	}
	
	@RequestMapping("/newFriends.do")
	public ModelAndView newFriends() throws Exception{
		List<FriendsVO> flist = friendsService.newFriends();
		System.out.println("------flist-------");
		System.out.println(flist);
		return new ModelAndView("newFriends.friends", "flist", flist);
	}
	
	@RequestMapping("/searchById.do")
	@ResponseBody
	public Map searchById(String search) throws Exception{
		System.out.println(search + "------searchById");
		MemberVO vo = friendsService.searchById(search);
		System.out.println(vo +"------------vo");
		String str;
		if(vo==null){
			str="검색결과가 없습니다.";
		}else{
			str="검색결과<br>";
			str += "아이디 : " + vo.getId() + " , 이름 : " + vo.getName() + ", 등급 : " +vo.getGrade();
		}
		HashMap map = new HashMap();
		map.put("str", str);
		map.put("vo", vo);
		
		return map;
	}
	
	@RequestMapping("/joinFriend.do")
	public ModelAndView joinFriend(String myid, String id) throws Exception{
		FriendsVO fvo = new FriendsVO();
		MemberVO vo = new MemberVO();
		MemberVO vo2 = new MemberVO();
		vo.setId(myid);
		fvo.setMyId(vo);
		vo2.setId(id);
		fvo.setFriends(vo2);
		
		String str="";
		str = friendsService.joinFriend(fvo);
		
		return new ModelAndView("joinFriend.friends", "str", str);
	}

	@RequestMapping("/deleteFriend.do")
	public ModelAndView deleteFriend(HttpSession session,String[] friend1) throws Exception{
		FriendsVO fvo = new FriendsVO();
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		friendsService.outFriends(friend1);
		return new ModelAndView("redirect:getFriendsList.do?id="+mvo.getId());
	}
	
	@RequestMapping("/giftPoint.do")
	public ModelAndView giftPoint(HttpSession session, int point, String[] id) throws Exception{
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		System.out.println("id------" + id);
		System.out.println("point------" + point);
		mvo = friendsService.giftPoint(mvo, id, point);
		
		session.setAttribute("mvo", mvo);
		return new ModelAndView("redirect:getFriendsList.do?id="+mvo.getId());
	}
	
	@RequestMapping("/sendMemosView.do")
	public ModelAndView sendMemosView(String id){
		//System.out.println("sendMemosView---------" + id);
		return new ModelAndView("sendMemosView.friends", "id", id);
	}
	
	@RequestMapping("/sendMemos.do")
	public ModelAndView sendMemos(MemoVO memoVO) throws Exception{
		System.out.println("memoVO---------" + memoVO);
		friendsService.sendMemos(memoVO);
		return new ModelAndView("sendMemos.friends");
	}
}
